package com.wendi.order.web;

import com.wendi.order.pojo.Order;
import com.wendi.order.pojo.User;
import com.wendi.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;
   @Autowired
   private RestTemplate restTemplate;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);

        //根据restTemplate来向另一个服务发送请求
        //第一个参数要的是调用另一个接口的地址，第二个参数要的是你调用后返回的类型
        User user = restTemplate.getForObject("http://userservice:8081/user/"+ order.getUserId(), User.class);
        //把得到的数据设置回order数据里
        order.setUser(user);

        return order;
    }
}
